﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;
using BC;
using BE;
using COMExcel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Threading;
namespace ModuleDuoc.Report
{
    public partial class frmTK_BienBanKiemKe : ModuleDuoc.Base.frmTK_Base
    {
        //string sql;
        int redCount;
        Zd_BienBanKiemKe be = new Zd_BienBanKiemKe();
        Zd_BienBanKiemKeBC bc = new Zd_BienBanKiemKeBC();
        public frmTK_BienBanKiemKe()
        {
            InitializeComponent();
        }
        
        private void frmTK_BienBanKiemKe_Load(object sender, EventArgs e)
        {
            this.dgvData.AutoGenerateColumns = false;
            setHoTen();
            dateThoiGian.Text = DateTime.Parse(this.dateThoiGian.Value.ToString("MM/yyyy")).AddMonths(-1).ToString();
        }

        private void setHoTen()
        {
            dgvDanhSach.Rows.Add("1", "Nguyễn Thị Mỹ Dung", "Phụ trách nhà thuốc - Chủ tịch hội đồng kiểm kê");
            dgvDanhSach.Rows.Add("2", "Nguyễn Ngọc Lâm", "Phụ trách kế toán - Thư ký");
            dgvDanhSach.Rows.Add("3", "Nguyễn Thị Thủy", "Phụ trách nhập thuốc - Thành viên");
            dgvDanhSach.Rows.Add("4", "Đỗ Thị Quyên", "Nhân viên nhà thuốc - Thành viên");
            dgvDanhSach.Rows.Add("5", "", "");
        }
        private void insertData()
        {
            string sql = "zD_BC_TONKHO";
            //string sql = "zD_BC_KiemKeThuoc";
            int thang = Int32.Parse(dateThoiGian.Text.Substring(0, 2));
            int nam = Int32.Parse(dateThoiGian.Text.Substring(3, 4));

            System.Data.SqlClient.SqlParameter tungay = new System.Data.SqlClient.SqlParameter();
            tungay.ParameterName = "@tungay";
            tungay.DbType = DbType.DateTime;
            tungay.Direction = ParameterDirection.Input;
            tungay.Value = "01/" + dateThoiGian.Text + " 00:00:00";

            System.Data.SqlClient.SqlParameter denngay = new System.Data.SqlClient.SqlParameter();
            denngay.ParameterName = "@denngay";
            denngay.DbType = DbType.DateTime;
            denngay.Direction = ParameterDirection.Input;
            denngay.Value = DateTime.DaysInMonth(nam, thang) + "/" + dateThoiGian.Text + " 23:59:00";
            System.Data.SqlClient.SqlParameter mavt;

            mavt = new System.Data.SqlClient.SqlParameter("@mavt", "");
            System.Data.SqlClient.SqlParameter manhom;
            manhom = new System.Data.SqlClient.SqlParameter("@manhom", "");
            System.Data.SqlClient.SqlParameter nhacc = new System.Data.SqlClient.SqlParameter("@nhacc", "");
            System.Data.SqlClient.SqlParameter wherein = new System.Data.SqlClient.SqlParameter("@wherein", "");
            SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.StoredProcedure, sql,
                tungay,
                denngay,
                mavt,
                manhom,
                nhacc,
                wherein);
        }
        /// <summary>
        /// Xem kết quả
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btnView_Click(object sender, EventArgs e)
        {
            base.btnView_Click(sender, e);
            insertData(); //đưa dữ liệu vào bảng tạm
            redCount = 0;
            string sql = "SELECT zdbt.mavach, zdbt.MAHANGHOA, zdbt.MAVT, zdbt.tenvt, zdbt.dvt, zdbt.solo, zdbt.nuocsx, " +
                            "CONVERT(VARCHAR,(zdbt.hansd), 103) AS hansd, zdbt.toncuoi,zdk.soluong, zdk.slhong, zdk.ghichu, zdk.thang, zdk.nam " +
                            "FROM zD_bcTonkhoth zdbt INNER JOIN zD_KIEMKE2012 zdk ON zdk.mavach = zdbt.mavach " +
                            "WHERE (zdbt.tondauky <> 0 OR zdbt.slnhap <> 0) AND zdk.thang = " + (Int32.Parse(dateThoiGian.Text.Substring(0, 2)) + 1).ToString() + 
                            " AND nam = " + dateThoiGian.Text.Substring(3, 4) + " " +
                            "ORDER BY zdbt.tenvt";

            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.dgvData.DataSource = dt;

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dgvData.Rows.Count; i++)
                {
                    dgvData.Rows[i].Cells["slhong"].Value = Int32.Parse(dgvData.Rows[i].Cells["soluong"].Value.ToString()) - Int32.Parse(dgvData.Rows[i].Cells["toncuoi"].Value.ToString());
                    if (dgvData.Rows[i].Cells["toncuoi"].Value.ToString() != dgvData.Rows[i].Cells["soluong"].Value.ToString())
                    {
                        dgvData.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                        redCount++;
                    }
                }
            }
            this.lblKhongKhop.Text = "Không khớp: " + redCount.ToString();
            this.lblKhop.Text = "Khớp số liệu: " + (dt.Rows.Count - Int32.Parse(redCount.ToString())).ToString();
        }

        /// <summary>
        /// Xuất Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btnExcel_Click(object sender, EventArgs e)
        {
           // xuatExcel();
        }

        public override void btnExportReport_Click(object sender, EventArgs e)
        {
            base.btnExportReport_Click(sender, e);
            insertData(); //đưa dữ liệu vào bảng tạm
            CapNhat();
            printReport();
        }
        private void printReport()
        {
            // Thông tin hành chính
            //string sql = "SELECT KiemKe_ID, BoYTe, BenhVien, Khoa, MS, So, Ten1, Ten2, Ten3, Ten4, Ten5, " +
            //                "ChucDanh1, ChucDanh2, ChucDanh3, ChucDanh4, ChucDanh5, Date1, Date2, ThoiGian, DeXuat " +
            //                "FROM zD_BienBanKiemKe " +
            //                "WHERE thoiGian = '" + dateThoiGian.Text + "'";

            //DataTable dt = new DataTable();
            //dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];

            //Lấy danh sách thuốc;
            string sql1 = "SELECT zdbt.mavach, zdbt.MAHANGHOA, zdbt.MAVT, zdbt.tenvt, zdbt.dvt, zdbt.solo, zdbt.nuocsx, " +
                            "CONVERT(VARCHAR,(zdbt.hansd), 103) AS hansd, zdbt.toncuoi,zdk.soluong, zdk.slhong, zdk.ghichu, zdk.thang, zdk.nam " +
                            "FROM zD_bcTonkhoth zdbt INNER JOIN zD_KIEMKE2012 zdk ON zdk.mavach = zdbt.mavach " +
                            "WHERE (zdbt.tondauky <> 0 OR zdbt.slnhap <> 0) AND zdk.thang = " + (Int32.Parse(dateThoiGian.Text.Substring(0, 2)) + 1).ToString() + 
                            " AND nam = " + dateThoiGian.Text.Substring(3, 4) + " " +
                            "ORDER BY zdbt.tenvt";
            DataTable dt1 = new DataTable();
            dt1 = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql1).Tables[0];
                   

            frmShowReport frm = new frmShowReport(2);
            frmShowReport.strC = frmShowReport.BC.BIENBANKIEMKETHUOC;
            frmShowReport._dt1 = dt1;
            frmShowReport._thoiGian = dateThoiGian.Text;
            frmShowReport._thang = (Int32.Parse(dateThoiGian.Text.Substring(0, 2)) + 1).ToString();
            frmShowReport._nam = dateThoiGian.Text.Substring(3, 4);

            //Thong tin
            frmShowReport._boYte = txtBoYte.Text;
            frmShowReport._benhVien = txtBenhVien.Text;
            frmShowReport._khoa = txtKhoa.Text;
            frmShowReport._ms = txtMS.Text;
            frmShowReport._so = txtSo.Text;
            frmShowReport._deXuat = txtDeXuat.Text;
            frmShowReport._date1 = date1.Text;
            frmShowReport._date2 = date2.Text;
            frmShowReport._ten1 = dgvDanhSach.Rows[0].Cells[1].Value.ToString(); 
            frmShowReport._ten2 = dgvDanhSach.Rows[1].Cells[1].Value.ToString(); 
            frmShowReport._ten3 = dgvDanhSach.Rows[2].Cells[1].Value.ToString(); 
            frmShowReport._ten4 = dgvDanhSach.Rows[3].Cells[1].Value.ToString();
            frmShowReport._ten5 = dgvDanhSach.Rows[4].Cells[1].Value.ToString();
            frmShowReport._chucDanh1 = dgvDanhSach.Rows[0].Cells[2].Value.ToString();
            frmShowReport._chucDanh2 = dgvDanhSach.Rows[1].Cells[2].Value.ToString();
            frmShowReport._chucDanh3 = dgvDanhSach.Rows[2].Cells[2].Value.ToString();
            frmShowReport._chucDanh4 = dgvDanhSach.Rows[3].Cells[2].Value.ToString();
            frmShowReport._chucDanh5 = dgvDanhSach.Rows[4].Cells[2].Value.ToString();
            frm.ShowDialog();
        }
        private void CapNhat()
        {
            be.BoYTe = txtBoYte.Text;
            be.BenhVien = txtBenhVien.Text;
            be.Khoa = txtKhoa.Text;
            be.MS = txtMS.Text;
            be.So = txtSo.Text;
            be.Ten1 = dgvDanhSach.Rows[0].Cells[1].Value.ToString();
            be.Ten2 = dgvDanhSach.Rows[1].Cells[1].Value.ToString();
            be.Ten3 = dgvDanhSach.Rows[2].Cells[1].Value.ToString();
            be.Ten4 = dgvDanhSach.Rows[3].Cells[1].Value.ToString();
            be.Ten5 = dgvDanhSach.Rows[4].Cells[1].Value.ToString();

            be.ChucDanh1 = dgvDanhSach.Rows[0].Cells[2].Value.ToString();
            be.ChucDanh2 = dgvDanhSach.Rows[1].Cells[2].Value.ToString();
            be.ChucDanh3 = dgvDanhSach.Rows[2].Cells[2].Value.ToString();
            be.ChucDanh4 = dgvDanhSach.Rows[3].Cells[2].Value.ToString();
            be.ChucDanh5 = dgvDanhSach.Rows[4].Cells[2].Value.ToString();
            be.Date1 = DateTime.Parse(date1.Value.ToString("dd/MM/yyyy HH:mm:ss"));
            be.Date2 = DateTime.Parse(date2.Value.ToString("dd/MM/yyyy HH:mm:ss"));
            be.ThoiGian = dateThoiGian.Value.ToString("MM/yyyy");//thông kê theo thang
            be.DeXuat = txtDeXuat.Text;
            be.TinhTrang = "1";
            be.Field5 = "KiemKe"; // Bien ban kiem ke

            if (txtKiemKe_ID.Text == "")
            {
                bc.Insert(be);
                //MessageBox.Show("Thêm mới thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
               // be.ThoiGian = dateThoiGian.Value.ToString("MM/yyyy");//thông kê theo thang
                be.KiemKe_ID = Int32.Parse(txtKiemKe_ID.Text);
                bc.Update(be);
               // MessageBox.Show("Cập nhật thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);      
            }
        }

        private void dateThoiGian_ValueChanged(object sender, EventArgs e)
        {
            fillAll();
        }

        private void fillAll()
        {
            //Zd_BienBanKiemKeBC bc = new Zd_BienBanKiemKeBC();
            //DataTable dt = new DataTable();
            //dt = bc.GetAll();
            string sql = "SELECT KiemKe_ID, BoYTe, BenhVien, Khoa, MS, So, Ten1, Ten2, Ten3, Ten4, Ten5, " +
                            "ChucDanh1, ChucDanh2, ChucDanh3, ChucDanh4, ChucDanh5, Date1, Date2, ThoiGian, DeXuat " +
                            "FROM zD_BienBanKiemKe " +
                            "WHERE Field5 = 'KiemKe' and thoiGian = '"+dateThoiGian.Text+"'";

            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            //this.dgvData.DataSource = dt;

            if (dt.Rows.Count > 0)
            {
                txtKiemKe_ID.Text = dt.Rows[0]["KiemKe_ID"].ToString();
                txtBoYte.Text = dt.Rows[0]["BoYTe"].ToString();
                txtBenhVien.Text = dt.Rows[0]["BenhVien"].ToString();
                txtKhoa.Text = dt.Rows[0]["Khoa"].ToString();
                txtMS.Text = dt.Rows[0]["MS"].ToString();
                txtSo.Text = dt.Rows[0]["So"].ToString();

                dgvDanhSach.Rows[0].Cells[0].Value = "1";
                dgvDanhSach.Rows[1].Cells[0].Value = "2";
                dgvDanhSach.Rows[2].Cells[0].Value = "3";
                dgvDanhSach.Rows[3].Cells[0].Value = "4";
                dgvDanhSach.Rows[4].Cells[0].Value = "5";

                dgvDanhSach.Rows[0].Cells[1].Value = dt.Rows[0]["Ten1"].ToString();
                dgvDanhSach.Rows[1].Cells[1].Value = dt.Rows[0]["Ten2"].ToString();
                dgvDanhSach.Rows[2].Cells[1].Value = dt.Rows[0]["Ten3"].ToString();
                dgvDanhSach.Rows[3].Cells[1].Value = dt.Rows[0]["Ten4"].ToString();
                dgvDanhSach.Rows[4].Cells[1].Value = dt.Rows[0]["Ten5"].ToString();

                dgvDanhSach.Rows[0].Cells[2].Value = dt.Rows[0]["ChucDanh1"].ToString();
                dgvDanhSach.Rows[1].Cells[2].Value = dt.Rows[0]["ChucDanh2"].ToString();
                dgvDanhSach.Rows[2].Cells[2].Value = dt.Rows[0]["ChucDanh3"].ToString();
                dgvDanhSach.Rows[3].Cells[2].Value = dt.Rows[0]["ChucDanh4"].ToString();
                dgvDanhSach.Rows[4].Cells[2].Value = dt.Rows[0]["ChucDanh5"].ToString();

                date1.Text = dt.Rows[0]["Date1"].ToString();
                date2.Text = dt.Rows[0]["Date2"].ToString();
                dateThoiGian.Text = dt.Rows[0]["ThoiGian"].ToString();
                txtDeXuat.Text = dt.Rows[0]["DeXuat"].ToString();
            }
            else
            {
                txtKiemKe_ID.Text = "";
            }
        }
    }
}
